package service

import (
	"ginblog/enum"
	"ginblog/model"
	"gorm.io/gorm"
)

type CommentService struct {
}

type CommentSerInterface interface {
	GetCommentList(pageSize int, pageNum int) ([]model.Comment, int64, enum.Code)
	CheckComment(commId int, date *model.Comment) enum.Code
	DeleteUser(commId int) enum.Code
	GetComment(commId int) (model.Comment, enum.Code)
	UncheckComment(id int, data *model.Comment) enum.Code
	CommentFront(id int, pageSize, pageNum int) ([]model.Comment, enum.Code, int64)
	AddComment(comment *model.Comment) enum.Code
}

// GetCommentList 后台所有获取评论列表
func (comm CommentService) GetCommentList(pageSize int, pageNum int) ([]model.Comment, int64, enum.Code) {
	var comments []model.Comment
	var total int64
	db.Find(&comments).Count(&total)
	err := db.Model(&comments).Limit(pageSize).Offset((pageNum - 1) * pageSize).Order("Created_At DESC").Select("comment.id, article.title,user_id,article_id, user.username, comment.content, comment.status,comment.created_at,comment.deleted_at").Joins("LEFT JOIN article ON comment.article_id = article.id").Joins("LEFT JOIN user ON comment.user_id = user.id").Scan(&comments).Error
	if err != nil {
		return comments, 0, enum.ERROR
	}
	return comments, total, enum.SUCCESS
}

// DeleteUser 删除评论
func (comm CommentService) DeleteUser(commId int) enum.Code {
	comment := model.Comment{}
	// 删除用户，软删除
	err := db.Where("id = ?", commId).Delete(&comment).Error
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}

// CheckComment 通过审核
func (comm CommentService) CheckComment(commId int, data *model.Comment) enum.Code {
	var comment model.Comment
	var res model.Comment
	var article model.Article
	var maps = make(map[string]interface{})
	maps["status"] = data.Status

	err := db.Model(&comment).Where("id = ?", commId).Updates(maps).First(&res).Error
	db.Model(&article).Where("id = ?", res.ArticleId).UpdateColumn("comment_count", gorm.Expr("comment_count + ?", 1))
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}

// GetComment 查询单个评论
func (comm CommentService) GetComment(commId int) (model.Comment, enum.Code) {
	var comment model.Comment
	err := db.Where("id = ?", commId).First(&comment).Error
	if err != nil {
		return comment, enum.ERROR
	}
	return comment, enum.SUCCESS
}

// UncheckComment 撤下评论
func (comm CommentService) UncheckComment(id int, data *model.Comment) enum.Code {
	var comment model.Comment
	var res model.Comment
	var article model.Article
	var maps = make(map[string]interface{})
	maps["status"] = data.Status

	err := db.Model(&comment).Where("id = ?", id).Updates(maps).First(&res).Error
	db.Model(&article).Where("id = ?", res.ArticleId).UpdateColumn("comment_count", gorm.Expr("comment_count - ?", 1))
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}

func (comm CommentService) CommentFront(id int, pageSize, pageNum int) ([]model.Comment, enum.Code, int64) {
	var commemt = []model.Comment{}
	var count int64
	db.Model(&commemt).Where("article_id = ? and status = 1", id).Count(&count)
	err := db.Where("article_id = ? and status = 1", id).Order("created_at desc").Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&commemt).Error
	if err != nil {
		return commemt, enum.ERROR, count
	}
	return commemt, enum.SUCCESS, count
}

func (comm CommentService) AddComment(comment *model.Comment) enum.Code {
	var data model.User
	db.Select("id").Where("username = ?", comment.Username).First(&data)
	comment.UserId = data.ID

	err := db.Create(&comment).Error
	if err != nil {
		return enum.ERROR
	}
	return enum.SUCCESS
}
